package xuegao.study.mall.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.web.domain.PmsProductCategoryNode;
import xuegao.study.mall.web.domain.PmsProductDetail;
import xuegao.study.mall.web.service.PmsPortalProductService;
import xuegao.study.mall.common.api.CommonPage;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.model.pms.PmsProduct;

import java.util.List;

@RestController
@RequestMapping("/product")
public class PmsPortalProductController {

    @Autowired
    private PmsPortalProductService pmsPortalProductService;

    @GetMapping("/search")
    public CommonResult<CommonPage<PmsProduct>> search(@RequestParam(required = false) String keyword,
                                                       @RequestParam(required = false) Long brandId,
                                                       @RequestParam(required = false) Long productCategoryId,
                                                       @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                       @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                                                       @RequestParam(required = false, defaultValue = "0") Integer sort) {
        List<PmsProduct> list = pmsPortalProductService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
        return CommonResult.success(CommonPage.restPage(list));
    }


    @GetMapping("/categoryTreeList")
    public CommonResult<List<PmsProductCategoryNode>> categoryTreeList() {
        List<PmsProductCategoryNode> list = pmsPortalProductService.categoryTreeList();
        return CommonResult.success(list);
    }

    @GetMapping("/detail/{id}")
    public CommonResult<PmsProductDetail> detail(@PathVariable Long id) {
        PmsProductDetail detail = pmsPortalProductService.detail(id);
        return CommonResult.success(detail);
    }
}
